import myAxios from "@/utils/myAxios"
import userStore from "@/store/modules/user"

const BaseUrl = '/api/good'

/**
 * 新增商品
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function addGood(goodInfo) {
    return myAxios.post(`${BaseUrl}/add`, {
        masterId: goodInfo.masterId,
        name: goodInfo.name,
        description: goodInfo.description,
        img: goodInfo.img,
        price: goodInfo.price,
        purchaseDate: goodInfo.purchaseDate,
        tagList: goodInfo.tagList
    }, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 更新商品
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function updateGoodById(id, goodInfo) {
    return myAxios.post(`${BaseUrl}/update/${id}`, {
        masterId: goodInfo.masterId,
        name: goodInfo.name,
        description: goodInfo.description,
        img: goodInfo.img,
        price: goodInfo.price,
        purchaseDate: goodInfo.purchaseDate,
        tagList: goodInfo.tagList
    }, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 通过id获取商品
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getGoodById(id) {
    return myAxios.get(`${BaseUrl}/get/${id}`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 通过id删除商品
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function deleteGoodById(id) {
    return myAxios.post(`${BaseUrl}/delete/${id}`, {}, {
        headers: {
            Authorization: "Bearer " + userStore.state.token, // TODO 这里先带着token，但是后端没有带权限校验
        }
    })
}

/**
 * 获取商品列表
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getGoodList(page, pageSize) {
    return myAxios.get(`${BaseUrl}/list?page=${page}&pageSize=${pageSize}`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}


/**
 * 获取商品列表
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getGoodListByCondition(page, pageSize, searchObj) {
    return myAxios.get(`${BaseUrl}/list?page=${page}&pageSize=${pageSize}&searchObj=${searchObj}`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 获取我的商品列表
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getGoodListByMasterId(page, pageSize) {
    return myAxios.get(`${BaseUrl}/list/my?page=${page}&pageSize=${pageSize}`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}


/**
 * 通过id点赞商品
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function postGoodThumb(goodId) {
    return myAxios.post(`${BaseUrl}/thumb/${goodId}`, {}, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}
